package com.hyk.commonLib.common.aop;

import com.hyk.commonLib.common.utils.eventHandler.BaseEventHandler;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

@Aspect
public class EventHandlerAspect {
    /**
     * @see BaseEventHandler.CheckContainer
     */
    @Around("execution(@com.hyk.commonLib.common.utils.eventHandler.BaseEventHandler.CheckContainer * *(..))")
    public Object checkContainer(ProceedingJoinPoint joinPoint) throws Throwable {
        Object target = joinPoint.getTarget();
        if (!(target instanceof BaseEventHandler)) {
            throw new RuntimeException("@CheckContainer 注解只能用于 BaseEventHandler 及 其子类 的方法上");
        }
        if (!(((BaseEventHandler<?, ?>) target).containerAvailable())) {
            return null;
        }
        if (((BaseEventHandler<?, ?>) target).getActivity() == null) {
            return null;
        }
        return joinPoint.proceed();
    }

    public static EventHandlerAspect aspectOf() {
        return new EventHandlerAspect();
    }
}
